package com.example.module_visitor.view

import android.app.AlertDialog
import android.text.Editable
import android.text.TextWatcher
import android.widget.PopupMenu
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.module_visitor.R
import com.example.module_visitor.adapter.VisitorAdapter
import com.example.module_visitor.adapter.VisitorItemAdapter
import com.example.module_visitor.databinding.ActivityVisitorBinding
import com.example.module_visitor.viewmodel.VisitorVIewModel
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.VisitorEntityItem
import dagger.hilt.android.AndroidEntryPoint
import java.util.*


@AndroidEntryPoint
@Route(path = RouterPath.APPOINTMENTS_URL)
class VisitorActivity : BaseActivity<ActivityVisitorBinding, VisitorVIewModel>() {
    val visitorAdapter: VisitorAdapter = VisitorAdapter()
    val visitorItemAdapter = VisitorItemAdapter()
    val visitorData = mutableListOf<VisitorEntityItem>()
    var mPosition = 0
    override fun initView() {
        //来访列表
        binding.visitorRv2.adapter = visitorAdapter
        binding.visitorRv2.layoutManager = LinearLayoutManager(this)
        vm.getVisitor()
        binding.visitorBack.setOnClickListener {
            finish()
        }
        binding.visitorRv1.adapter = visitorItemAdapter
        binding.visitorRv1.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        visitorItemAdapter.setOnItemClickListener { _, _, position ->
            visitorItemAdapter.setIndex(position)
            mPosition = position
            vm.getVisitor()
            visitorItemAdapter.notifyDataSetChanged()
        }

        vm.visitorSuccess.observe(this){
            visitorAdapter.data.clear()
            visitorData.clear()
            if(mPosition!=0){
                visitorData.addAll(it.filter { item->
                    item.visitStatus == visitorItemAdapter.data[mPosition]
                })
                visitorAdapter.data.addAll(visitorData.reversed())
                visitorAdapter.notifyDataSetChanged()
            }else{
                visitorData.addAll(it)
                visitorAdapter.data.addAll(visitorData.reversed())
                visitorAdapter.notifyDataSetChanged()
            }

        }
        //搜索
        binding.visitorEt.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                visitorAdapter.data.clear()
                val tt = s.toString().trim()
                val filterList = visitorData.filter {
                    it.visitorName.contains(tt, ignoreCase = true)
                }
                visitorAdapter.data.addAll(filterList)
                visitorAdapter.notifyDataSetChanged()
            }

        })
        //删除
        deleteVisitor()
        //添加
        binding.visitorBnt.setOnClickListener {
            ARouter.getInstance().build(RouterPath.ADD_APPOINTMENTS_URL).navigation()
        }

        visitorAdapter.setOnItemClickListener { adapter, view, position ->
            val popupMenu = PopupMenu(this,view)
            popupMenu.menuInflater.inflate(R.menu.visitor_menu,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { item ->
                when(item.itemId){
                    R.id.visit_approve ->{
                        vm.getApprove(visitorAdapter.data[position].id)
                        true
                    }
                    R.id.visit_reject ->{
                        vm.getReject(visitorAdapter.data[position].id)
                        true
                    }
                    else -> onOptionsItemSelected(item)
                }
            }
            popupMenu.show()

        }



    }

    override fun initData() {
        visitorItemAdapter.data.add("全部状态")
        visitorItemAdapter.data.add("待审核")
        visitorItemAdapter.data.add("已通过")
        visitorItemAdapter.data.add("驳回")
        visitorItemAdapter.data.add("已完成")
        visitorItemAdapter.notifyDataSetChanged()
        vm.deleteVisitSuccess.observe(this){
            ToastUtils.showLong("删除成功")

            vm.getVisitor()
            visitorAdapter.notifyDataSetChanged()
        }
        vm.rejectSuccess.observe(this){
            ToastUtils.showLong("已驳回")
            visitorAdapter.data.clear()
            vm.getVisitor()
            visitorAdapter.notifyDataSetChanged()
        }
        vm.approveSuccess.observe(this){
            ToastUtils.showLong("审核通过")
            visitorAdapter.data.clear()
            vm.getVisitor()
            visitorAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getVisitor()
    }
    fun deleteVisitor(){
        //删除
        visitorAdapter.setOnItemLongClickListener { adapter, view, position ->
            val id = visitorAdapter.data[position].id

            AlertDialog.Builder(this)
                .setTitle("删除来访提醒")
                .setMessage("确定要删除来访吗？")
                .setPositiveButton("确定"){dialog,_ ->
                    Toast.makeText(this, "已删除", Toast.LENGTH_SHORT).show()
                    vm.getDeleteVisit(id)
                    dialog.dismiss()
                }
                .setNegativeButton("取消"){dialog,_ ->
                    dialog.dismiss()
                }
                .create()
                .show()
            return@setOnItemLongClickListener false
        }
    }

}